﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Waker.Common;
using DevExpress.XtraEditors;

namespace Waker.Devices
{
    public partial class FormViedoView : Base.FormBase
    {
        public FormViedoView()
        {
            InitializeComponent();
        }

        DataTable _Dt;
        string _UserName = string.Empty;
        string _Pwd = string.Empty;
        private void FormViedoView_Load(object sender, EventArgs e)
        {
            _Dt = new BAL.BAL_Device().GetTableByTableName("T_NVR");
            SimpleButton firstBtn = null;
            if (_Dt != null && _Dt.Rows.Count > 0)
            {
                foreach (DataRow dr in _Dt.Rows)
                {
                    SimpleButton btn = new SimpleButton();
                    btn.Dock = DockStyle.Left;
                    btn.Text = dr["NVR_Name"].ToString();
                    btn.Width = btn.Text.Length * 15;
                    btn.Click += new EventHandler(btn_Click);
                    btn.Tag = dr;
                    this.groupNVR.Controls.Add(btn);
                    if (firstBtn == null)
                        firstBtn = btn;
                }
            }
            btn_Click(firstBtn, null);
        }

        void btn_Click(object sender, EventArgs e)
        {
            var btn = sender as SimpleButton;
            if (btn == null || btn.Tag == null)
                return;
            var dr = btn.Tag as DataRow;
            if (dr == null)
                return;
            var url = string.Format("http://{0}/doc/page/login.asp", dr["NVR_IP"]);
            _UserName = dr["NVR_UserName"].ToString();
            _Pwd = dr["NVR_Pwd"].ToString();
            this.webBrowser1.Url = new Uri(url);
        }
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (this.webBrowser1.Url.ToString().EndsWith("login.asp"))
            {
                if (webBrowser1.Document == null)
                    return;
                var txtUseName = webBrowser1.Document.GetElementById("loginUserName");
                if (txtUseName != null)
                    txtUseName.InnerText = _UserName;//fill name
                var txtPwd = webBrowser1.Document.GetElementById("loginPassword");
                if (txtPwd != null)
                    txtPwd.InnerText = _Pwd;//fill pwd
                //txtUseName.SetAttribute("Value", _Pwd);
                //var txtUseName = webBrowser1.Document.GetElementById("UserName");
                //if (txtUseName != null)
                //{
                //    //txtUseName.SetAttribute("Text", _UserName);//fill name
                //    //txtUseName.SetAttribute("Text", _UserName);//fill name
                //    txtUseName.InnerText = _UserName;//fill name
                //}
                //var txtPwd = webBrowser1.Document.GetElementById("Password");
                //if (txtPwd != null)
                //{   //txtPwd.InnerText = _Pwd;//fill pwd
                //    //txtUseName.SetAttribute("Text", _Pwd);
                //    //txtUseName.SetAttribute("Text", _Pwd);
                //    txtUseName.InnerText = _Pwd;
                //}
                var spans = webBrowser1.Document.GetElementsByTagName("span");
                if (spans.Count < 6)
                    return;
                if (spans[5] != null)
                {
                    spans[5].InvokeMember("click");
                }
                //var spans = webBrowser1.Document.GetElementsByTagName("div");
                //if (spans.Count < 9)
                //    return;
                //if (spans[8] != null)
                //{
                //    var divs = spans[8].GetElementsByTagName("div");
                //    if (divs.Count < 3)
                //        return;
                //    var login = divs[2];
                //    login.InvokeMember("click");
                //}
                return;
            }
            if (this.webBrowser1.Url.ToString().EndsWith("main.asp"))//preview.asp
            {
                var htmlIFrame = this.webBrowser1.Document.Window.Frames["ContentFrame"];
                if (htmlIFrame != null && htmlIFrame.Document != null)
                {
                    //var htmlPlay = htmlIFrame.Document.GetElementById("play");
                    //if (htmlPlay != null)
                    //    htmlPlay.InvokeMember("click");
                    var htmlFullscreen = htmlIFrame.Document.GetElementById("fullscreen");
                    if (htmlFullscreen != null)
                    {
                        //htmlFullscreen.Enabled = false;
                        htmlFullscreen.Style = "display:none;";
                    }
                    //var txtDeviceName = htmlIFrame.Document.GetElementById("Device");
                    //if (txtDeviceName != null)
                    //    txtDeviceName.InnerText = _UserName;//fill name
                }
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
